<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\DB;

class RollMaster extends Model
{
    use SoftDeletes;
    protected $table = 'roll_master';

    public function getProductTypeIdAttribute()
    {
        $fm = FilmTypeMaster::withTrashed()->find($this->film_type_id);
        return @$fm->product_type_id ?: 0;
    }

    public function getPID()
    {
        $fm = FilmTypeMaster::withTrashed()->find($this->film_type_id);
        return @$fm->product_type_id ?: 0;
    }

    public function getResidueLengthAttribute()
    {

    }

    public function getUsedLengthAttribute()
    {
        $roll_number = $this->roll_number;
        # 质保使用长度，不考虑假如质保删除，但是卷号未删除
        $l1 = WarrantyToVehicleCoverage::where('roll_number', $roll_number)->sum('length');
        $l2 = PrecutToVehicleCoverage::where('roll_number', $roll_number)->sum('length');

        return ($l1 + $l2);

    }

    public function getIsAllocatedAttribute()
    {
        return !!count($this->sales()->get());
    }

    public function getAllocatedZhAttribute()
    {
        if (count($this->sales()->get())) {
            return ts('allocated');
        }
        return ts('unallocated');
    }

    public function getDealerNameAttribute()
    {

    }

    public function getWarrantyRecordsAttribute()
    {
        $query = "SELECT wvc.warranty_id from warranty_to_vehicle_coverage as wvc left join warranty as w on wvc.warranty_id=w.id where wvc.roll_number=? group by wvc.warranty_id";
        $data = DB::select($query, [$this->roll_number]);
        return $data;
    }

    public function getAllocatedRecordsAttribute()
    {
        $roll_number = $this->roll_number;
        $warrantys = Warranty::whereIn('id', WarrantyToVehicleCoverage::where('roll_number', $roll_number)->get('id'))
            ->get(['id', 'user_id']);

        return $warrantys;
        $warrantys = Warranty::whereHas('warranty_to_vehicle_coverage', function (Builder $query) use ($roll_number) {
            $query->where('roll_number', $roll_number);
        })->get(['id', 'user_id']);
        return $warrantys;
    }

    public function sales()
    {
        return $this->hasMany('App\Models\RollSales', 'roll_id');
    }
    /*
     * 质保记录
     * 仅显示已通过质保记录*/
    public function warrantyRecords()
    {
        $data = DB::table('warranty as w')
            ->leftJoin('warranty_to_vehicle_coverage as wvc', 'w.id', '=', 'wvc.warranty_id')
            ->where('wvc.roll_number', $this->roll_number)
            ->leftJoin('precut as p', 'w.pre_id', 'p.id')
            ->whereNull('w.deleted_at')
            ->whereNull('wvc.deleted_at')
            ->where('w.approved', '!=', 3)
            ->where('w.id', '>=', 1)
            ->orderByDesc('w.id')
            ->select([
                'wvc.roll_number',
                'wvc.length',
                'p.id as pid',
                'p.precut_id as p_id',
                'w.id as wid',
                'w.warranty_id as w_id',
                DB::raw('w.approved_date as effective_time'),
            ])
            ->get();
        return @$data ?: [];
    }

    public function precutRecords()
    {
       $data = DB::table('precut as p')
            ->leftJoin('precut_to_vehicle_coverage as tvc', 'p.id', '=', 'tvc.precut_kit_sale_id')
            ->where('tvc.roll_number', $this->roll_number)
            ->whereNull('p.deleted_at')
            ->whereNull('tvc.deleted_at')
            ->orderByDesc('p.id')
            ->select([
                'tvc.roll_number',
                'tvc.length',
                'p.id as pid',
                'p.precut_id as p_id',
                'p.id'
            ])
            ->get();
        return @$data ?: [];
    }

    /*
     * 销售记录*/
    public function salesRecords($type=1)
    {
        $rs = DB::table('roll_sales as rs')
            ->leftJoin('users as x', 'x.id', 'rs.sold_to_user_id')
            ->leftJoin('users as y', 'y.id', 'x.creator_id')
            ->leftJoin('users as z', 'z.id', 'rs.sold_by_user_id')
            ->where('rs.type', 1)
            ->where('rs.roll_id', $this->id)
            ->orderByDesc('rs.id');
        $data = $rs->get([
            'rs.*',
            DB::raw('if(x.user_type=3, x.abbr, "-") as jxsmc'), 'x.user_type',
            DB::raw('if(x.user_type=3, y.abbr, x.abbr) as sdmc'), DB::raw('z.abbr as bfpr')
        ]);
        return @$data ?: [];
    }

    /*
     * 调货记录*/
    public function transferRecords($type=1)
    {

        $model = DB::table('roll_transfer as rt')
            ->leftJoin('users as y', 'y.id', 'rt.transfer_by_user_id')
            ->leftJoin('users as x', 'x.id', 'rt.transfer_to_user_id')
            ->leftJoin('users as c', 'c.id', 'rt.created_by')
            ->leftJoin('users as yd', 'yd.id', 'y.creator_id')
            ->leftJoin('users as xd', 'xd.id', 'x.creator_id')
            ->where('rt.roll_id', $this->id)
            ->whereNull('rt.deleted_at')
            ->select([
                'rt.*',
                DB::raw('y.abbr as ygmsmc'),
                DB::raw('x.abbr as xgmsmc'), 'c.abbr as fqr',
                DB::raw('if(x.user_type=3, xd.abbr, x.abbr) as xgmssd'),
                DB::raw('if(y.user_type=3, yd.abbr, y.abbr) as ygmssd'),
                DB::raw('if(x.user_type=3, xd.abbr, x.abbr) as xsdmc'),
                DB::raw('if(y.user_type=3, yd.abbr, y.abbr) as ysdmc'),
                'y.user_type as ygms_type',
                'x.user_type as xgms_type',
                'y.user_type as yuser_type',
                'x.user_type as xuser_type',
            ])
            ->orderByDesc('rt.id');

        if ($type == 1) {
            $model = $model->where('approved', 1);
        }
        $data = $model->get();
        return @$data ?: [];
    }

    public function returnRecords($type=1)
    {
        $model = DB::table('roll_return as rr')
            ->leftJoin('users as y', 'y.id', 'rr.user_id')
            ->leftJoin('users as x', 'x.id', 'y.creator_id')
            ->where('rr.roll_id', $this->id)
            ->whereNull('rr.deleted_at')
            ->select(['rr.*',
                // DB::raw('y.abbr as jxsmc'),
                DB::raw('if(y.user_type!=3, "-", y.abbr) as jxsmc'),
                DB::raw('if(y.user_type=3, x.abbr, y.abbr) as sdmc'),
                'y.user_type as jxs_type',
            ]);
        if ($type == 1) {
            $model = $model->where('rr.status', 1);
        }
        $data = $model->get();
        return @$data ?: [];
    }

    public function getProductId()
    {
        $model = FilmTypeMaster::find($this->film_type_id);
        return @$model->id ?: 0;
    }


}
